﻿/**
 * 应用程序源码文件
 */
#ifndef STT_HMIAPP_CPP_
#define STT_HMIAPP_CPP_

/*********************
 *   头文件
 *********************/
#include "HmiApp.h"
/*PAGES_HEAD_BEGIN*/
#include "pages/Frm01.h"
#include "pages/Frm02.h"
#include "pages/Frm03.h"
#include "pages/Frm04.h"
#include "pages/Frm05.h"
/*PAGES_HEAD_END*/

using namespace stt;

/********************************
 *
 *  应用程序入口
 *
 *  注意：IDE自动生成，用户勿修改
 *
 ********************************/
IApplication * AppMain(IRuntime* env, char **argv){
	return new HmiApp(env);
}

string getAppArgs() {
	return "{\"r\":0}";
}

/********************************
 *
 *  应用程序类
 *
 *  注意：用户可添加代码
 *
 ********************************/
HmiApp::HmiApp(IRuntime * rt) : HmiAppBase(rt) {
}

HmiApp::~HmiApp() {
}

/********************************
 *
 *  应用构建前调用此函数
 *
 *  注意：用户可添加代码
 *
 ********************************/
void HmiApp::onInit(){
	//用户在此添加客户化代码
}

/********************************
 *
 *  应用构建函数
 *
 *  注意：IDE自动生成，用户勿修改
 *
 ********************************/
void HmiApp::construct() {
	//注册字体文件
	fm->registerFontFile("文泉驿微米黑", "./fnt/wenquanyiweimihei.ttf");
	//设备注册
	dm->addDevice("通用物联网云平台2", R"({"clientid":"tuyalink_26929afc06198ce5een9ve","dll":"TlsMqttDriver","DeviceId":"通用物联网云平台2","cert":"","cafile":"./res/pic/tuya.pem","processTimeout":"3000","issim":"true","password":"81ebc7a7e44d247fd50da1eb327c02499d77b8e1b060537e2aebc799033d6e6e","numIterations":"1","port":"8883","host":"m1.tuyacn.com","oneConnetTime":"300","addr":"hb40155f.ala.cn-hangzhou.emqxsl.cn","Protocol":"COMMONMQTT","if":"ETHNET","loopDelay":"100","key":"","username":"26929afc06198ce5een9ve|signMethod=hmacSha256,timestamp=1742392502,secureMode=1,accessType=1"})");
	dm->addDevice("通用物联网云平台2", R"({"clientid":"tuyalink_26929afc06198ce5een9ve","dll":"TlsMqttDriver","DeviceId":"通用物联网云平台2","cert":"","cafile":"/customer/prm/res/pic/tuya.pem","processTimeout":"3000","issim":"false","password":"81ebc7a7e44d247fd50da1eb327c02499d77b8e1b060537e2aebc799033d6e6e","numIterations":"1","port":"8883","host":"m1.tuyacn.com","oneConnetTime":"300","topic_sub":"","addr":"hb40155f.ala.cn-hangzhou.emqxsl.cn  ","Protocol":"COMMONMQTT","if":"ETHNET","loopDelay":"100","key":"","username":"26929afc06198ce5een9ve|signMethod=hmacSha256,timestamp=1742392502,secureMode=1,accessType=1"})");
	dm->addDevice("aws", R"({"clientid":"","dll":"TlsMqttDriver","DeviceId":"aws","cert":"./res/pic/aws-wfb2.crt","cafile":"./res/pic/aws-ca1.pem","processTimeout":"3000","issim":"true","password":"","numIterations":"1","port":"8883","host":"a1mp3hdlpio4yf-ats.iot.us-east-2.amazonaws.com","oneConnetTime":"300","Protocol":"COMMONMQTT","if":"ETHNET","key":"./res/pic/aws-wfb2.key","loopDelay":"100","username":""})");
	dm->addDevice("aws", R"({"clientid":"","dll":"TlsMqttDriver","DeviceId":"aws","cert":"/customer/prm/res/pic/aws-wfb2.crt","cafile":"/customer/prm/res/pic/aws-ca1.pem","processTimeout":"3000","issim":"false","password":"","numIterations":"1","port":"8883","host":"a1mp3hdlpio4yf-ats.iot.us-east-2.amazonaws.com","oneConnetTime":"300","Protocol":"COMMONMQTT","if":"ETHNET","key":"/customer/prm/res/pic/aws-wfb2.key","loopDelay":"100","username":""})");
	dm->addDevice("emqx", R"({"clientid":"","dll":"TlsMqttDriver","DeviceId":"emqx","cert":"","cafile":"./res/pic/emqxsl-ca.crt","processTimeout":"3000","issim":"true","password":"test1","numIterations":"1","port":"8883","host":"a1b31a61.ala.cn-hangzhou.emqxsl.cn","oneConnetTime":"300","Protocol":"COMMONMQTT","if":"ETHNET","key":"","loopDelay":"100","username":"test1"})");
	dm->addDevice("emqx", R"({"clientid":"","dll":"TlsMqttDriver","DeviceId":"emqx","cert":"","cafile":"/customer/prm/res/pic/emqxsl-ca.crt","processTimeout":"3000","issim":"false","password":"test1","numIterations":"1","port":"8883","host":"a1b31a61.ala.cn-hangzhou.emqxsl.cn","oneConnetTime":"300","Protocol":"COMMONMQTT","if":"ETHNET","key":"","loopDelay":"100","username":"test1"})");
	dm->addDevice("aliyun", R"({"clientid":"k28pphzN9iq.stt1|securemode=2,signmethod=hmacsha256,timestamp=1736674401841|","dll":"TlsMqttDriver","DeviceId":"aliyun","cert":"","cafile":"","processTimeout":"3000","issim":"true","password":"61f7e524fd48c0c677d66228557b6b2985051739af063033c73edd2403f86c2a","numIterations":"1","port":"1883","host":"iot-06z00drv6qe8xc4.mqtt.iothub.aliyuncs.com","oneConnetTime":"300","Protocol":"COMMONMQTT","if":"ETHNET","key":"","loopDelay":"100","username":"stt1&k28pphzN9iq"})");
	//设备变量
	vm->add("变量_9", DataType::D_VCHAR, R"({"dvc":"通用物联网云平台2","a":"tylink/26929afc06198ce5een9ve/thing/property/report","c":"PubVariable","t":"0","id":"v9","l":"1024"})");
	vm->add("变量_10", DataType::D_VCHAR, R"({"dvc":"通用物联网云平台2","a":"tylink/26929afc06198ce5een9ve/thing/property/set","c":"SubVariable","t":"0","id":"v10","l":"1024"})")->onTriHandler(Event::READY, this, (EHandler) &HmiApp::v10_tri_cb);
	vm->add("变量_11", DataType::D_VCHAR, R"({"dvc":"通用物联网云平台2","a":"任意","c":"SubTopic","t":"0","id":"v11","l":"1024"})");
	vm->add("aws1", DataType::D_VCHAR, R"({"dvc":"aws","a":"aws","c":"PubVariable","t":"0","id":"v13","l":"1024"})");
	vm->add("aws2", DataType::D_VCHAR, R"({"dvc":"aws","a":"aws","c":"SubVariable","t":"0","id":"v14","l":"1024"})")->onTriHandler(Event::READY, this, (EHandler) &HmiApp::v14_tri_cb);
	vm->add("aws3", DataType::D_VCHAR, R"({"dvc":"aws","a":"aws","c":"SubTopic","t":"0","id":"v15","l":"1024"})");
	vm->add("emqx1", DataType::D_VCHAR, R"({"dvc":"emqx","a":"testtopic","c":"PubVariable","t":"0","id":"v17","l":"1024"})");
	vm->add("emqx2", DataType::D_VCHAR, R"({"dvc":"emqx","a":"testtopic","c":"SubVariable","t":"0","id":"v18","l":"1024"})");
	vm->add("emqx3", DataType::D_VCHAR, R"({"dvc":"emqx","a":"testtopic","c":"SubTopic","t":"0","id":"v19","l":"1024"})");
	vm->add("aliyun1", DataType::D_VCHAR, R"({"dvc":"aliyun","a":"/sys/k28pphzN9iq/stt1/thing/event/property/post","c":"PubVariable","t":"0","id":"v21","l":"1024"})");
	vm->add("aliyun2", DataType::D_VCHAR, R"({"dvc":"aliyun","a":"/sys/k28pphzN9iq/stt1/thing/service/property/set|/sys/k28pphzN9iq/stt1/thing/event/property/post_reply","c":"SubVariable","t":"0","id":"v22","l":"1024"})");
	vm->add("aliyun3", DataType::D_VCHAR, R"({"dvc":"aliyun","a":"aliyun3","c":"SubTopic","t":"0","id":"v23","l":"1024"})");
	//常规变量
	vm->add("test1", DataType::T_INT, R"({"id":"v25"})");
	//事件采集设置
	stt::op::EventMgr::getInstance(rt)->load();
	//背光、屏保
	pm->setBackLightHoldingTime(0, 50);

}

IPagePtr HmiApp::buildPage(string id, uint8_t mode, STTWidget *target) {
	if (id == "Frm02")
		return new Frm02(this, target, "Frm02", "Frm02");
	if (id == "Frm03")
		return new Frm03(this, target, "Frm03", "Frm03");
	if (id == "Frm04")
		return new Frm04(this, target, "Frm04", "Frm04");
	if (id == "Frm05")
		return new Frm05(this, target, "Frm05", "Frm05");
	return new Frm01(this, target, "Frm01", "Frm01");
}

/********************************
 *
 *  构建应用后调用此函数
 *
 *  注意：用户可添加代码
 *
 ********************************/
void HmiApp::onReady(){
	//用户在此添加客户化代码
	string newusr = "test1";//要修改的用户名
	string newpwd = "test1";//要修改的密码
#if __WIN32__
	string settings = R"({"clientid":"","dll":"TlsMqttDriver","DeviceId":"emqx","cert":"","cafile":"./res/pic/emqxsl-ca.crt","processTimeout":"3000","issim":"true","password":"{pwd}","numIterations":"1","port":"8883","host":"a1b31a61.ala.cn-hangzhou.emqxsl.cn","oneConnetTime":"300","Protocol":"COMMONMQTT","if":"ETHNET","key":"","loopDelay":"100","username":"{usr}"})";
#else
	string settings = R"({"clientid":"","dll":"TlsMqttDriver","DeviceId":"emqx","cert":"","cafile":"/customer/prm/res/pic/emqxsl-ca.crt","processTimeout":"3000","issim":"false","password":"{pwd}","numIterations":"1","port":"8883","host":"a1b31a61.ala.cn-hangzhou.emqxsl.cn","oneConnetTime":"300","Protocol":"COMMONMQTT","if":"ETHNET","key":"","loopDelay":"100","username":"{usr}"})";
#endif
	settings = dm->getDevice("emqx")->getSettings();
	settings = StringUtil::replace(settings, "{usr}", newusr);
	settings = StringUtil::replace(settings, "{pwd}", newpwd);
	dm->getDevice("emqx")->setSettings(settings);

}

/********************************
 *
 *  应用退出前调用此函数
 *
 *  注意：用户可添加代码
 *
 ********************************/
void HmiApp::onQuit(){
	//用户在此添加客户化代码
}

void HmiApp::v10_tri_cb(uint16_t code, LvEvent e) {
	//变量[变量_10]值变化触发函数
	string v;
	vm->getChar("变量_10", v);
	cout<<v<<endl;//处理接收到的消息
}

void HmiApp::v14_tri_cb(uint16_t code, LvEvent e) {
	//变量[aws2]值变化触发函数
	string v;
	vm->getChar("aws2", v);
	cout<<v<<endl;//处理接收到的消息
}
#endif /* STT_HMIAPP_CPP_ */


